<?php
// +----------------------------------------------------------------------
// | 海报
// +----------------------------------------------------------------------
namespace app\app\controller\soft;
use app\app\Appbase;
use base\files\Poster;
use base\pay\WxApi;
use think\Image;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;


class Spread extends Appbase 
{
	
	/**
	* 生成海报
	* /app/soft/spread/imgs
	* 需要登录 
	* @is_need_login true
	*/
	public function imgs($sid=''){
		$uid = request() ->userId;
		try {
			$url = getSystem("extension_poster_url") .'?sid='. ($sid ?: $uid);
			$bg = $bg ?: getSystem("extension_poster_tpl_bg");
			$size = $size ?: getSystem("extension_poster_tpl_size");
			$size = $size < 50 ? 50 : $size;
			$x = $x ?: getSystem("extension_poster_tpl_x");
			$y = $y ?: getSystem("extension_poster_tpl_y");
			$result = Poster::generate($bg,$url,$size,$x,$y,true);
			if($result){
				return $this->ReSucess($result);
			}else{
				return $this->ReError();
			}
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}

	/**
	* 小程序海报二维码
	* /app/soft/spread/wxAppQrCode
	* 需要登录 
	* @is_need_login true
	*/
	public function wxAppQrCode($path='',$scene=''){
		
		$uid = $scene ?: request() ->userId;
		
		//系统文件地址
		$sycPath = 'public/images/bill/wxapp/'.$uid.'_';
		$qrCodePath = "public/images/bill/wxapp/qr_code.png";
		
		try {
			
			//参数
			$path = $path ?: (getSystem("extension_poster_url") ?: 'pages/index/index');
			$bg = $bg ?: getSystem("extension_poster_tpl_bg");
			$size = $size ?: getSystem("extension_poster_tpl_size");
			$size = $size < 80 ? 80 : $size;
			$x = $x ?: getSystem("extension_poster_tpl_x");
			$y = $y ?: getSystem("extension_poster_tpl_y");
			
			// 获取二维码
			$qrCodeImg = WxApi::getAppQRCode($path,$uid);
			$res = file_put_contents($qrCodePath,$qrCodeImg);
			if($res){
				$image = Image::open($qrCodePath);
				$image->thumb($size,$size)->save($qrCodePath);
			}else{
				return $this->ReError(); 
			}
			
			//生成海报
			copy($bg,$sycPath.'fx.jpg');
			$image = Image::open($sycPath.'fx.jpg');
			$image->water($qrCodePath,[$x,$y])->save($sycPath.'image.png'); 
			return $this->ReSucess(APP_PATH.'/'.$sycPath.'image.png');
			
        } catch (\Exception $e) {
        	return $this->ReError( $e->getMessage() );
        }
	}
	
	/**
	* 推广商品
	* /app/soft/spread/goods
	* 需要登录 
	* @is_need_login true
	*/
	public function goods($goods_id,$path=''){
		try {
			
			if(!$goods = model('goods')->getModel()->get($goods_id)){
				return $this->ReSucess('商品不存在！');
			}
			
			$userInfo= request() ->userInfo;
			$uid = request() ->userId;
			$paths = APP_PATH.'wx/inlet?scene='.$uid.'&path='.urlencode($path);
			
			//临时文件地址
			$path = public_path().'/images/bill/poster/';
			$savePath = 'images/bill/poster/'.$uid.'_goods.png';
			$ttf = public_path().'/simsun.ttf';
			
			//背景
			copy($path.'001.jpg',$path.'fx-bg.jpg');
			$image = Image::open($path.'fx-bg.jpg');
			$image->crop(320,550)->water($path.'fx-bg.jpg',[0,320])->save($savePath); 
			//商品图
			copy($goods->goods_img,$path.'fx-goods.jpg');
			Image::open($path.'fx-goods.jpg')->thumb(290,200,2)->radius(5)->save($path.'fx-goods.jpg');
			$image->water($path.'fx-goods.jpg',[15,15])->save($savePath); 
			//价格			
			$image->text($goods->goods_name,$ttf,12,'#000',[10,225])->save($savePath);
			$image->text('现价：￥'.$goods->sale_old_price,$ttf,13,'#ff0000',[12,256])->save($savePath);
			$image->text('原价：￥'.$goods->price,$ttf,11,'#000',[160,258])->save($savePath);
			//个人图
			copy($userInfo['avatarurl'],$path.'fx-avatarurl.jpg');
			Image::open($path.'fx-avatarurl.jpg')->thumb(60,60)->radius(30)->save($path.'fx-avatarurl.jpg');
			$image->water($path.'fx-avatarurl.jpg',[15,290])->save($savePath); 
			//名字			
			$image->text($userInfo['nickname'],$ttf,12,'#000',[85,300])->save($savePath);
			$image->text('向你推荐产品，长按识别二维码，立即购买',$ttf,8.9,'#000',[85,330])->save($savePath);
			//二维码
			$qrCode = Poster::qrCode($paths,170);
			$image->water($qrCode,[70,350])->save($savePath); 
			
//			Header("Content-type: image/png"); 
//			die(imagejpeg( imagecreatefromjpeg($savePath) ));
			return $this->ReSucess( APP_PATH.$savePath .'?t='.time() );
		
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 我的二维码
	* /app/soft/spread/myQrCode
	* 需要登录 
	* @is_need_login true
	*/
	public function myQrCode($desc=''){
		try {
			$userInfo= request() ->userInfo;
			$paths = APP_PATH.'wx/inlet?scene='.request() ->userId;
			$desc = $desc ?: $userInfo['nickname'];
			
			//临时文件地址
			$path = public_path().'/images/bill/poster/';
			$savePath = 'images/bill/poster/'.$uid.'_user_02.png';
			$ttf = public_path().'/simsun.ttf';
			
			//背景
			copy($path.'001.jpg',$path.'fx-bg.jpg');
			$image = Image::open($path.'fx-bg.jpg');
			$image->crop(310,420)->radius(10)->water($path.'fx-bg.jpg',[0,300])->save($savePath); 
			//个人图
			copy($userInfo['avatarurl'],$path.'fx-avatarurl.jpg');
			Image::open($path.'fx-avatarurl.jpg')->thumb(80,80)->radius(40)->save($path.'fx-avatarurl.jpg');
			$image->water($path.'fx-avatarurl.jpg',[20,20])->save($savePath); 
			//名字			
			$image->text($desc,$ttf,15,'#000',[115,55])->save($savePath);
			//二维码
			$qrCode = Poster::qrCode($paths,160);
			$image->water($qrCode,[70,120])->save($savePath); 
			$image->text('注：长按图片可以关注',$ttf,12,'#000',[80,300])->save($savePath);
			
//			Header("Content-type: image/png"); 
//			die(imagejpeg( imagecreatefromjpeg($savePath) ));
			return $this->ReSucess( APP_PATH.$savePath .'?t='.time() );
		
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}

